package com.xiao.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.xiao.train.common.exception.BusinessException;
import com.xiao.train.common.exception.BusinessExceptionEnum;
import com.xiao.train.common.resp.CommonResp;
import com.xiao.train.common.resp.MemberLoginResp;
import com.xiao.train.common.util.JwtUtil;
import com.xiao.train.common.util.SnowUtil;
import com.xiao.train.member.domain.Member;
import com.xiao.train.member.domain.MemberExample;
import com.xiao.train.member.mapper.MemberMapper;
import com.xiao.train.member.req.MemberLoginReq;
import com.xiao.train.member.req.MemberRegisterReq;
import com.xiao.train.member.req.MemberSendCodeReq;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MemberService {

    @Resource
    MemberMapper memberMapper;

    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    public Long register(MemberRegisterReq req) {
        String mobile = req.getMobile();
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if (CollUtil.isNotEmpty(members)) {
//            return members.get(0).getId();
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowUtil.getId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    public MemberLoginResp login(MemberLoginReq req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        Member member = selectByMobile(mobile);
        //手机号不存在
        if (ObjectUtil.isNull(member)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        if (!"8888".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(member, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }

    private Member selectByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if (members.isEmpty())
            return null;
        return members.get(0);
    }

    public void sendCode(MemberSendCodeReq req) {
        String mobile = req.getMobile();
        if (selectByMobile(mobile) == null) {
            log.info("手机号{}不存在, 插入一条记录", mobile);
            Member member = new Member();
            member.setId(SnowUtil.getId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        } else {
            log.info("手机号{}已存在", mobile);
        }
        //随机生成短信验证码
        String code = RandomUtil.randomString(4);
        code = "8888";
        log.info("生成短信验证码:{}", code);
        //保存日志
        log.info("保存验证码日志");
        //对接短信通道
        log.info("对接短信通道");
    }
}
